/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wasolicitudesfiec.dataproviders.administracion;

import com.sun.data.provider.DataProviderException;
import com.sun.data.provider.FieldKey;
import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.AbstractTableDataProvider;
import com.sun.data.provider.impl.IndexRowKey;
import entidad.Persona;
import entidad.Rol;
import java.util.Vector;
import wasolicitudesfiec.logicacontrol.Logica;

/**
 *
 * @author kabad
 */
public class Dp_Persona extends AbstractTableDataProvider {
    //registro

    private Vector<Persona> listaRg_Persona = new Vector<Persona>();

    public Dp_Persona() {
        FieldKey key;
        key = new FieldKey("id");
        key.setDisplayName("id");
        addFieldKey(key);

        key = new FieldKey("nombre");
        key.setDisplayName("Nombre");
        addFieldKey(key);

        key = new FieldKey("cedula");
        key.setDisplayName("Cédula");
        addFieldKey(key);

        key = new FieldKey("matricula");
        key.setDisplayName("Matrícula");
        addFieldKey(key);


        key = new FieldKey("correo");
        key.setDisplayName("Correo");
        addFieldKey(key);

        key = new FieldKey("titulo");
        key.setDisplayName("Título");
        addFieldKey(key);

        key = new FieldKey("usuario");
        key.setDisplayName("usuario");
        addFieldKey(key);

        key = new FieldKey("roles");
        key.setDisplayName("Roles");
        addFieldKey(key);
    }

    @Override
    public Class getType(FieldKey fieldKey) throws DataProviderException {
		return String.class;
    }

    @Override
    public boolean isReadOnly(FieldKey fieldKey) throws DataProviderException {
        return false;
    }

    @Override
    public int getRowCount() throws DataProviderException {
        return this.getListaRg_Persona().size();
    }
    Logica logica = new Logica();

    @Override
    public Object getValue(FieldKey fieldKey, RowKey row) throws DataProviderException {

		if (fieldKey==null)
			return "";
        int indice = ((IndexRowKey) row).getIndex();
        Persona registro = (Persona) this.getListaRg_Persona().get(indice);
        Vector<Rol> roles = null;
        String nombreRol = "  ";
        roles = registro.getRoles();
        if (roles != null) {
            for (Rol rol : roles) {
                nombreRol += rol.getRol() + ",  ";
            }
        }
		String fieldId= fieldKey.getFieldId();
        if (fieldId.equals("usuario")) {
            return registro.getUsuario() == null ? "No Disponible" : registro.getUsuario();
        } else if (fieldId.equals("nombre")) {
            return registro.getNombres() == null ? "No Disponible" : registro.getApellidos() + " " + registro.getNombres();
        } else if (fieldId.equals("cedula")) {
            return registro.getCedula() == null ? "No Disponible" : registro.getCedula();
        } else if (fieldId.equals("matricula")) {
            return registro.getMatricula() == null ? "No Disponible" : registro.getMatricula();
        } else if (fieldId.equals("id")) {
            return registro.getIdPersona() == 0 ? "No Disponible" : registro.getIdPersona();
        } else if (fieldId.equals("correo")) {
            return registro.getCorreo() == null ? "No Disponible" : registro.getCorreo();
        } else if (fieldId.equals("titulo")) {
            return registro.getTituloAcademico() == null ? "No Disponible" : registro.getTituloAcademico();
        } else if (fieldId.equals("roles")) {
            return roles == null ? "No Disponible" : nombreRol;
        } else {
            return registro.toString();
        }

    }

    @Override
    public void setValue(FieldKey fieldKey, RowKey row, Object value) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canInsertRow(RowKey beforeRow) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public RowKey insertRow(RowKey beforeRow) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canAppendRow() throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public RowKey appendRow() throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean canRemoveRow(RowKey row) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void removeRow(RowKey row) throws DataProviderException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Vector<Persona> getListaRg_Persona() {
        return listaRg_Persona;
    }

    public void setListaRg_Persona(Vector<Persona> listaRg_Persona) {
        this.listaRg_Persona = listaRg_Persona;
    }
}
